<template>
  <el-dialog
    ref="refDlg"
    :title="formData.id ? '编辑' : '新增'"
    :visible.sync="dialogShow"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :before-close="clearForm"
    width="600px"
    @close="closeDialog"
  >
    <div>
      <el-form ref="refForm" :rules="formRules" :model="formData" size="mini" label-width="100px">
        <el-form-item label="评价类别:" prop="categoryName">
          <input-with-dialog
            v-model="typeArr"
            :props="{label:'name',value:'id'}"
            :config="{
              title:'评价类型',
              width:'600px',
              interfacePath:`/slm-admin/api/v1/decorateCategory/page`,
              method:'post',
              filtersRaw:[{id:'enable',property:'enable',operator:'=',value:'1'}],
              paramsRaw:{},
              setting:[
                {prop:'name',title:'评价类别'},
                {prop:'decorateName',title:'评价类型'}
              ]
            }"
            @change="handleChange"
          />
        </el-form-item>
        <el-form-item label="评价类型:" prop="decorateName">
          <el-input v-model="formData.decorateName" disabled placeholder="请选择评价类别" />
        </el-form-item>
        <el-form-item label="评价条目:" prop="name">
          <el-input v-model="formData.name" type="textarea" placeholder="请输入评价条目" />
        </el-form-item>
        <el-form-item label="启用状态:" prop="enable">
          <el-radio-group v-model="formData.enable">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">停用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer">
      <el-button size="mini" @click="closeDialog">取消</el-button>
      <el-button size="mini" type="primary" @click="submitForm">确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { saveEvaluateItemSetting } from '@/api/marketService/evaluateItemSetting'
export default {
  name: 'DemoEditForm',
  props: {
    visible: {
      type: Boolean,
      default: false,
      required: true
    },
    detailData: {
      type: [String, Object],
      default: ''
    }
  },
  data() {
    return {
      submiting: false,
      formData: {
        categoryId: '',
        categoryName: '',
        decorateId: '',
        decorateName: '',
        enable: '',
        id: null,
        name: ''
      },
      typeArr: [],
      formRules: {
        name: [{ required: true, message: '请输入', trigger: 'change' }],
        categoryName: [{ required: true, message: '请选择', trigger: 'change' }],
        enable: [{ required: true, message: '请选择', trigger: 'change' }]
      }
    }
  },

  computed: {
    dialogShow: {
      get() {
        return this.visible
      },
      set(val) {
        this.$emit('update:visible', val)
      }
    }
  },
  created() {
    const data = this.detailData
    if (data && data.id) {
      const { id, name, categoryName, categoryId, decorateId, decorateName, enable } = data
      this.formData = { id, name, categoryName, categoryId, decorateId, decorateName, enable }
      this.typeArr = [{ value: this.formData.categoryId, label: this.formData.categoryName }]
    }
  },

  methods: {
    handleChange(data) {
      if (data && data.length) {
        this.$set(this.formData, 'categoryId', data[0].id)
        this.$set(this.formData, 'categoryName', data[0].name)
        this.$set(this.formData, 'decorateId', data[0].decorateId)
        this.$set(this.formData, 'decorateName', data[0].decorateName)
      } else {
        this.$set(this.formData, 'categoryId', '')
        this.$set(this.formData, 'categoryName', '')
        this.$set(this.formData, 'decorateId', '')
        this.$set(this.formData, 'decorateName', '')
      }
    },
    closeDialog() {
      this.dialogShow = false
      this.clearForm()
    },
    clearForm(done) {
      this.formData = {
        categoryId: '',
        categoryName: '',
        decorateId: '',
        decorateName: '',
        enable: '',
        id: null,
        name: ''
      }
      this.$refs.refForm && this.$refs.refForm.clearValidate()
      done && done()
    },
    // 提交表单处理
    submitForm() {
      if (this.submiting) return
      this.$refs['refForm'].validate((valid) => {
        if (valid) {
          this.submiting = true
          saveEvaluateItemSetting(this.formData).then(res => {
            this.submiting = false
            if (res.success === 1) {
              this.$message({
                message: '操作成功',
                type: 'success'
              })
              this.closeDialog()
              const isNew = !this.formData.id // 是否新增数据 true (新增则刷新列表到第一页)/ false (非新增则刷新当前页)
              this.$emit('success', isNew)
            }
          }).catch(() => {
            this.submiting = false
          })
        }
      })
    }
  }
}
</script>
<style lang='scss' scoped>
</style>
